using MicroMsg.Common.Algorithm;
using MicroMsg.Common.Event;
using MicroMsg.Common.Timer;
using MicroMsg.Common.Utils;
using MicroMsg.Network;
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace MicroMsg.Scene
{
	public class NetSceneCrashReport
	{
		private const string TAG = "NetSceneCrashReport";
		private const string ReportHost = "short.weixin.qq.com";
		private TimerObject mTimerObject;
		private HttpClient mHttpClient;
		public void doScene(CrashInfo crashInfo)
		{
			if (crashInfo == null || crashInfo.crashData == null || crashInfo.crashData.get_Length() <= 0)
			{
				Log.smethod_1("NetSceneCrashReport", " cannot report crash with invalid args.");
				return;
			}
			if (this.mHttpClient != null)
			{
				Log.smethod_1("NetSceneCrashReport", " report crash already.");
				return;
			}
			string input = string.Format("weixin#$(){0}{1}", crashInfo.crashVersion, crashInfo.crashData.get_Length());
			string text = MD5Core.GetHashString(input, Encoding.get_UTF8()).ToLower();
			string text2 = crashInfo.crashVersion.ToString("x");
			string cmdUri = string.Format("http://{0}/cgi-bin/stackreport?version={1}&filelength={2}&sum={3}&reporttype={4}", new object[]
			{
				"short.weixin.qq.com",
				text2,
				crashInfo.crashData.get_Length(),
				text,
				(uint)crashInfo.crashType
			});
			Log.smethod_5("NetSceneCrashReport", "prepare to report crash ....");
			byte[] array = this.EncodeCrashPack(Util.StringToByteArray(crashInfo.crashData), Util.StringToByteArray(text));
			this.mHttpClient = new HttpClient(cmdUri, 1, null);
			this.mHttpClient.mDefineHeader += new DefineHttpHeaderDelegate(this.onDefineHttpHeader);
			this.mHttpClient.send(array, array.Length, null);
			if (this.mTimerObject == null)
			{
				this.mTimerObject = TimerService.addTimer(1, new EventHandler(this.onTimerHandler));
				this.mTimerObject.start();
			}
			Log.smethod_4("Network", "wait crash report response.... ");
		}
		public void onTimerHandler(object sender, EventArgs e)
		{
			byte[] array = null;
			int num = 0;
			string text = null;
			SocketError socketError = this.mHttpClient.receive(ref array, ref num, ref text);
			if (socketError == 11004 && this.mTimerObject.FireCount < 20)
			{
				return;
			}
			if (this.mTimerObject.FireCount >= 20)
			{
				Log.smethod_1("NetSceneCrashReport", " crash report not return , timeout .");
			}
			this.mTimerObject.stop();
			this.mTimerObject = null;
			this.mHttpClient.close();
			this.mHttpClient = null;
			Log.smethod_4("Network", "crash report over. ");
			EventCenter.postEvent(EventConst.ON_NETSCENE_CRASHREPORT_OVER, null, null);
		}
		public void onDefineHttpHeader(HttpWebRequest request)
		{
			request.set_Method("POST");
			request.set_ContentType("application/x-www-form-urlencoded");
			request.set_UserAgent("Mozilla/4.0");
			request.set_Accept("*/*");
			request.get_Headers().set_Item("Host", "short.weixin.qq.com");
		}
		private byte[] EncodeCrashPack(byte[] data, byte[] byte_0)
		{
			byte[] destBytes = null;
			if (!Zlib.Compress(data, ref destBytes))
			{
				Log.smethod_1("NetSceneCrashReport", " crash data:  compress failed.");
				return null;
			}
			byte[] array = DES.EncryptBytes(destBytes, byte_0, 1);
			if (array == null)
			{
				Log.smethod_5("NetSceneCrashReport", "crash data : des encrypt failed");
				return null;
			}
			return array;
		}
		public static void test()
		{
			CrashInfo crashInfo = new CrashInfo();
			crashInfo.crashType = ECrashReportType.Blocked;
			crashInfo.crashVersion = 1627389952;
			crashInfo.userName = "halenwp7";
			crashInfo.crashData = "crash testcrash testcrash testcrash testcrash testcrash testcrash testcrash testcrash testcrash testcrash testcrash testcrash testcrash testcrash test";
			NetSceneCrashReport netSceneCrashReport = new NetSceneCrashReport();
			netSceneCrashReport.doScene(crashInfo);
		}
	}
}
